package com.xg.te.plugin;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.alibaba.fastjson.JSONObject;
import com.jarveis.frame.lang.StringUtil;
import com.xg.te.plugin.bean.Entry;

/**
 * @desc XML数据插件
 * @author liuguojun
 * @date 2015-05-24
 */
public class XmlDataPlugin implements Plugin {

	private static final Logger logger = Logger.getLogger(XmlDataPlugin.class);

	public Map execute(JSONObject jsonObj) {
		Map reMap = new HashMap();
		try {
			String data = jsonObj.getString("data");
			if (StringUtil.isBlank(data)) {
				throw new Exception("parameter data is empty");
			}
			
			Element root = getRoot(new File(data));

			List<Element> elements = root.elements();
			for (Element element : elements) {
				Entry entry = parseElement(element);
				reMap.put(entry.getKey(), entry.getValue());
			}

		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
		}

		return reMap;
	}

	/**
	 * 解析标签
	 * 
	 * @param element 
	 * @return Entry
	 */
	private Entry parseElement(Element element) {
		Entry entry = new Entry();

		entry.setKey(element.getName());
		if (!StringUtil.isBlank(element.getText())) {
			entry.setValue(element.getText());
		} else {
			List<Element> elements = element.elements();
			List list = new ArrayList(elements.size());
			Map map = new HashMap(elements.size());

			for (Element ele : elements) {
				Entry child = parseElement(ele);
				if (child.getValue() instanceof String) {
					map.put(child.getKey(), child.getValue());
				} else if (child.getValue() instanceof List) {
					map.put(child.getKey(), child.getValue());
				} else if (child.getValue() instanceof Map) {
					list.add(child.getValue());
				}
			}

			if (!map.isEmpty()) {
				entry.setValue(map);
			} else if (!list.isEmpty()) {
				entry.setValue(list);
			}

		}

		return entry;
	}

	/**
	 * 获取根节点
	 * 
	 * @param file
	 * @return
	 */
	private Element getRoot(File file) {
		Element root = null;

		InputStream is = null;
		try {
			is = new FileInputStream(file);
			SAXReader reader = new SAXReader();
			reader.setValidation(false);
			Document document = reader.read(is);

			root = document.getRootElement();
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException ex) {
					logger.error(ex.getMessage(), ex);
				}
			}
		}

		return root;
	}


	public static void main(String[] args) {
		JSONObject jsonObj = new JSONObject();
		jsonObj.put("data", "D:\\Projects\\shweb_te\\test\\data\\agent.xml");

		XmlDataPlugin plugin = new XmlDataPlugin();
		Map map = plugin.execute(jsonObj);
		logger.info(map.toString());
	}

}
